package com.easylinkin.linkappapi.openapi.service.impl;

import com.easylinkin.linkappapi.openapi.service.ApplicationCopyDirectService;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import com.easylinkin.linkappapi.space.entity.LinkappArea;
import com.easylinkin.linkappapi.space.entity.LinkappSpace;
import com.easylinkin.linkappapi.space.mapper.LinkappAreaMapper;
import com.easylinkin.linkappapi.space.mapper.LinkappSpaceMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: linkapp-group-cloud
 * @description: 空间区域拷贝
 * @author: chenkaixuan
 * @create: 2021-09-06 17:24
 */
@Slf4j
@Order(1)
@Service
public class ApplicationCopySpaceAreaDirectServiceImpl implements ApplicationCopyDirectService {
    @Resource
    LinkappAreaMapper linkappAreaMapper;
    @Resource
    LinkappSpaceMapper linkappSpaceMapper;

    @Override
    public RestMessage copy(HashMap<String, String> screenmap, HashMap<String, String> map, LinkappUser newLinkappUser, LinkappUser oldLinkappUser) {
        return null;
    }

    @Override
    public RestMessage copy(LinkappUser newLinkappUser, LinkappUser oldLinkappUser) {
        if (newLinkappUser == null || oldLinkappUser == null) {
            return RestBuilders.failureMessage().setMessage("用户为空");
        }
        RestMessage restMessage = RestBuilders.successBuilder().build();
        //查询空间
        LinkappSpace linkappSpace = new LinkappSpace();
        linkappSpace.setTenantId(oldLinkappUser.getTenantId());
        List<LinkappSpace> linkappSpaces = linkappSpaceMapper.selectLinkappSpaceListGlobal(linkappSpace);
        if (linkappSpaces == null || linkappSpaces.size() <= 0) {
            return restMessage;
        }
        //查询区域
        LinkappArea linkappArea = new LinkappArea();
        List<String> spaces = linkappSpaces.stream().map(LinkappSpace::getId).collect(Collectors.toList());
        linkappArea.setSpaceIds(spaces);
        List<LinkappArea> linkappAreaList = linkappAreaMapper.selectLinkappAreaListAllGlobal(linkappArea);
        Map<String, List<LinkappArea>> linkappAreaListMap = linkappAreaList.stream().collect(Collectors.groupingBy(LinkappArea::getSpaceId));
        linkappAreaList = null;
        //保存空间
        Date date = new Date();
        String tenantId = newLinkappUser.getTenantId();
        List<LinkappArea> finalLinkappAreaList = new ArrayList<>();
        //保存区域父子映射关系
        int size = (int) (linkappAreaListMap.keySet().size() * 0.75) + 1;
        Map<String, String> fathersubMapper = new HashMap<>(size);
        //数据整理
        List<LinkappArea> finalLinkappAreaList1 = finalLinkappAreaList;
        linkappSpaces = linkappSpaces.stream().map(m -> {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            //修改区域空间节点ID
            if (linkappAreaListMap.get(m.getId()) != null) {
                List<LinkappArea> linkappAreas = linkappAreaListMap.get(m.getId());
                linkappAreas = linkappAreas.stream().map(a -> {
                    String areaid = null;
                    if(m.getId().equals(a.getId())){
                        areaid=uuid;
                    }else{
                        areaid = UUID.randomUUID().toString().replaceAll("-", "");
                    }
                    //存储新旧映射关系
                    fathersubMapper.put(a.getId(), areaid);
                    a.setId(areaid);
                    a.setSpaceId(uuid);
                    a.setTenantId(tenantId);
                    return a;
                }).collect(Collectors.toList());
                finalLinkappAreaList1.addAll(linkappAreas);
            }
            m.setId(uuid);
            m.setTenantId(tenantId);
            m.setCreateTime(date);
            return m;
        }).collect(Collectors.toList());
        //批量新增
        int batchSize = 100;
        if (linkappSpaces != null && linkappSpaces.size() > 0) {
            for (int idx = 0; idx < linkappSpaces.size(); idx += batchSize) {
                List<LinkappSpace> list = linkappSpaces.subList(idx, Math.min(linkappSpaces.size(), idx + batchSize));
                linkappSpaceMapper.batchInsert(list);
            }
        }
        if (finalLinkappAreaList != null && finalLinkappAreaList.size() > 0) {
            finalLinkappAreaList = finalLinkappAreaList.stream().map(m -> {
                //解决区域父子ID问题
                if (StringUtils.isEmpty(m.getParentId())) {
                    return m;
                }
                if (fathersubMapper.get(m.getParentId()) == null) {
                    return m;
                }
                //老父类值替换为新父类值
                m.setParentId(fathersubMapper.get(m.getParentId()));
                return m;
            }).collect(Collectors.toList());
            for (int idx = 0; idx < finalLinkappAreaList.size(); idx += batchSize) {
                List<LinkappArea> list = finalLinkappAreaList.subList(idx, Math.min(finalLinkappAreaList.size(), idx + batchSize));
                linkappAreaMapper.batchInsert(list);
            }
        }
        log.info("空间区域拷贝完成");
        return restMessage;
    }


}
